package com.lideru.blog.controlle;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lideru.blog.entity.Comment;
import com.lideru.blog.entity.User;
import com.lideru.blog.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserContrller {

    @Autowired
    private IUserService userService;
    @PostMapping("login")
    public String login(String username, String password, HttpSession session){
        User user = userService.login(username, password);
        if (user!=null){
            session.setAttribute("user",user);
            return "redirect:/";
        }
        return "login";
    }

    @RequestMapping("logout")
    public String quit( HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:/";
    }
    @RequestMapping("personal")
    public String personal(Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user!=null) model.addAttribute("user",user);
        else model.addAttribute("user",null);
        return "personal";
    }

}
